package com.ruoyi.project.purchase.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.inventory.domain.RyInventory;
import com.ruoyi.project.inventory.mapper.RyInventoryMapper;
import com.ruoyi.project.purchase.domain.RyDepotDetail;
import com.ruoyi.project.purchase.domain.RyDepotHead;
import com.ruoyi.project.purchase.domain.RyPurchaseOrder;
import com.ruoyi.project.purchase.domain.pojo.DepotDetailBean;
import com.ruoyi.project.purchase.domain.pojo.DepotHeadItem;
import com.ruoyi.project.purchase.mapper.RyDepotDetailMapper;
import com.ruoyi.project.purchase.mapper.RyDepotHeadMapper;
import com.ruoyi.project.purchase.mapper.RyPurchaseOrderMapper;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.warehouse.domain.RyDepot;
import com.ruoyi.project.warehouse.domain.RyDepotItem;
import com.ruoyi.project.warehouse.mapper.RyDepotItemMapper;
import com.ruoyi.project.warehouse.mapper.RyDepotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname infcController
 * @Description TODO
 * @Date 2022/9/23 18:04
 * @Created by You
 */

//@Anonymous
@RestController
@RequestMapping("/infcOrder")
public class InfcControllerOrder {
    @Autowired
    private RyDepotHeadMapper ryDepotHeadMapper;

    @Autowired
    private RyDepotDetailMapper ryDepotDetailMapper;

    @Autowired
    private RyDepotMapper ryDepotMapper;

    @Autowired
    private RyPurchaseOrderMapper ryPurchaseOrderMapper;


    @Autowired
    private RyDepotItemMapper ryDepotItemMapper;

    @Autowired
    private RyInventoryMapper ryInventoryMapper;


    /**
     * --采购订单
     * 新增采购订单及采购订单子表信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/addDepotHeadAndDetail")
    public AjaxResult addDepotHeadAndDetail(@RequestBody DepotHeadItem depotHeadItem)
    {
//        System.out.println("depotDetailBean---->" + depotDetailBean);
        System.out.println("linkHeadId---->" + depotHeadItem.getLinkHeadId());
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //判断是否有子表数据
        List<RyDepotItem> tableData = depotHeadItem.getTableData();
        if(tableData == null || tableData.size() ==0){
            return AjaxResult.error("子表数据不存在");
        }

        //新增采购订单信息
        RyPurchaseOrder ryPurchaseOrder = new RyPurchaseOrder();
        ryPurchaseOrder.setNumber(depotHeadItem.getNumber());//单据号
        ryPurchaseOrder.setContractNumber(depotHeadItem.getContractNumber());//采购合同号
        ryPurchaseOrder.setOrganId(depotHeadItem.getOrganId());//供应商id
        ryPurchaseOrder.setFileName(depotHeadItem.getFileName());
        ryPurchaseOrder.setSalesMan(depotHeadItem.getSalesMan());
        ryPurchaseOrder.setCreateTime(DateUtils.getNowDate());//创建时间
        ryPurchaseOrder.setCreator(user.getUserId());//操作员
        ryPurchaseOrder.setRemark(depotHeadItem.getRemark());
        ryPurchaseOrder.setStatus("0");//默认未审核
        ryPurchaseOrder.setShippingStatus("0");//默认未发货
        ryPurchaseOrder.setType("其他");
        ryPurchaseOrder.setSubType("采购订单");

        //判断是否关联了项目总清单
        if(depotHeadItem.getLinkHeadId() != null) {//关联项目
            ryPurchaseOrder.setLinkHeadId(depotHeadItem.getLinkHeadId());//关联项目id
            ryPurchaseOrder.setProjectHeadId(depotHeadItem.getLinkHeadId());
        }
        int res = ryPurchaseOrderMapper.insertRyPurchaseOrder(ryPurchaseOrder);
        if(res > 0){
            //遍历明细数据，更新单据明细数据
            for(RyDepotItem ryDepotItem : tableData){
                ryDepotItem.setHeaderId(ryPurchaseOrder.getId());//设置刚生成的采购订单id

                //判断物品是否存在物品库中
                RyInventory ryInventory = ryInventoryMapper.selectInfoByNameSpeci(ryDepotItem.getInventorySpecifications(), ryDepotItem.getInventoryName());
                if(ryInventory == null){
                    RyInventory ryInventory2 = new RyInventory();
                    // 物品名称
                    ryInventory2.setName(ryDepotItem.getInventoryName());
                    // 规格
                    ryInventory2.setSpecifications(ryDepotItem.getInventorySpecifications());
                    // wbs编码
                    ryInventory2.setWbs(ryDepotItem.getWbs());
                    // 品牌
                    if (ryDepotItem.getBrand() == null) {
                        ryInventory2.setBrand("/");
                    } else {
                        ryInventory2.setBrand(ryDepotItem.getBrand());
                    }
                    // 供应商ID
                    // ryInventory2.setSupplyId(ryDepotItem.getSupplyId().toString());
                    // 供应商
                    ryInventory2.setSupply(ryDepotItem.getSupply());
                    // 装填
                    ryInventory2.setStatus("0");
                    // 操作员
                    ryInventory2.setOperator(ryDepotItem.getInOperateMan());
                    // 单位
                    ryInventory2.setUnit("件");
                    // 单价
                    ryInventory2.setUnitPrice(ryDepotItem.getUnitPrice());
                    // 库位
                    ryInventory2.setLocation(ryDepotItem.getLocation());
                    ryInventoryMapper.insertRyInventory(ryInventory2);
                    ryDepotItem.setInventoryId(ryInventory2.getId());
                }
                ryDepotItemMapper.insertRyDepotItem(ryDepotItem);
            }
        }
        return AjaxResult.success("新增成功");
    }

    /**
     * --采购订单
     * 修改购订单及采购订单子表信息
     */
    @RequestMapping("/editDepotHeadAndDetail")
    public AjaxResult editDepotHeadAndDetail(@RequestBody DepotHeadItem depotHeadItem)
    {
        System.out.println("depotDetailBean---->" + depotHeadItem);
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();

        //判断是否有子表数据
        List<RyDepotItem> tableData = depotHeadItem.getTableData();
        if(tableData == null || tableData.size() ==0){
            return AjaxResult.error("子表数据不存在");
        }

        //修改主表数据
        RyPurchaseOrder ryPurchaseOrder = new RyPurchaseOrder();
        ryPurchaseOrder.setId(depotHeadItem.getId());//主键，用于定位修改数据
        ryPurchaseOrder.setDeliverTime(depotHeadItem.getDeliverTime());
        ryPurchaseOrder.setContractNumber(depotHeadItem.getContractNumber());
        ryPurchaseOrder.setOperTime(DateUtils.getNowDate());
        ryPurchaseOrder.setFileName(depotHeadItem.getFileName());
        ryPurchaseOrder.setRemark(depotHeadItem.getRemark());
        //修改
        ryPurchaseOrderMapper.updateRyPurchaseOrder(ryPurchaseOrder);

        //将子表数据先删除，根据主表id删除
        ryDepotItemMapper.deleteDepotItemByHeadId(ryPurchaseOrder.getId());
        //遍历明细数据，新增单据明细数据
        for(RyDepotItem ryDepotItem : tableData){
            ryDepotItem.setHeaderId(ryPurchaseOrder.getId());//设置刚生成的采购订单id

            //判断物品是否存在物品库中
            RyInventory ryInventory = ryInventoryMapper.selectInfoByNameSpeci(ryDepotItem.getInventorySpecifications(), ryDepotItem.getInventoryName());
            if(ryInventory == null){
                RyInventory ryInventory2 = new RyInventory();
                // 物品名称
                ryInventory2.setName(ryDepotItem.getInventoryName());
                // 规格
                ryInventory2.setSpecifications(ryDepotItem.getInventorySpecifications());
                // wbs编码
                ryInventory2.setWbs(ryDepotItem.getWbs());
                // 品牌
                if (ryDepotItem.getBrand() == null) {
                    ryInventory2.setBrand("/");
                } else {
                    ryInventory2.setBrand(ryDepotItem.getBrand());
                }
                // 供应商ID
                // ryInventory2.setSupplyId(ryDepotItem.getSupplyId().toString());
                // 供应商
                ryInventory2.setSupply(ryDepotItem.getSupply());
                // 装填
                ryInventory2.setStatus("0");
                // 操作员
                ryInventory2.setOperator(ryDepotItem.getInOperateMan());
                // 单位
                ryInventory2.setUnit("件");
                // 单价
                ryInventory2.setUnitPrice(ryDepotItem.getUnitPrice());
                // 库位
                ryInventory2.setLocation(ryDepotItem.getLocation());
                ryInventoryMapper.insertRyInventory(ryInventory2);
                ryDepotItem.setInventoryId(ryInventory2.getId());
            }
            ryDepotItemMapper.insertRyDepotItem(ryDepotItem);
        }
        return AjaxResult.success("修改成功");
    }


    /**
     * --采购订单
     * 获取采购订单主表及子表详细信息
     */
    @GetMapping(value = "/selectRyDepotDetailById/{id}")
    public AjaxResult selectRyDepotDetailById(@PathVariable("id") Long id)
    {

        DepotHeadItem depotHeadItem = new DepotHeadItem();
        RyPurchaseOrder ryPurchaseOrder = ryPurchaseOrderMapper.selectRyPurchaseOrderById(id);
        //主表数据
        depotHeadItem.setId(ryPurchaseOrder.getId());
        depotHeadItem.setProjectCode(ryPurchaseOrder.getProjectCode());//项目代码
        depotHeadItem.setProjectName(ryPurchaseOrder.getProjectName());//项目名

        depotHeadItem.setNumber(ryPurchaseOrder.getNumber());//单据号
        depotHeadItem.setContractNumber(ryPurchaseOrder.getContractNumber());//采购合同号

        depotHeadItem.setCreator(ryPurchaseOrder.getCreator());//操作员
        depotHeadItem.setOperName(ryPurchaseOrder.getOperName());//操作员名称

        depotHeadItem.setCreateTime(ryPurchaseOrder.getCreateTime());
        depotHeadItem.setOperTime(ryPurchaseOrder.getOperTime());
        depotHeadItem.setFileName(ryPurchaseOrder.getFileName());
        depotHeadItem.setSalesMan(ryPurchaseOrder.getSalesMan());

        depotHeadItem.setOrganId(ryPurchaseOrder.getOrganId());//供应商id
        depotHeadItem.setCompanyName(ryPurchaseOrder.getCompanyName());//供应商/客户名
        depotHeadItem.setLinkHeadId(ryPurchaseOrder.getLinkHeadId());//关联项目id

        depotHeadItem.setDeliverTime(ryPurchaseOrder.getDeliverTime());
        depotHeadItem.setRemark(ryPurchaseOrder.getRemark());

        //子表数据
        List<RyDepotItem> tableData = ryDepotItemMapper.selectRyDepotItemByDepotId(id);
        depotHeadItem.setTableData(tableData);

        return AjaxResult.success(depotHeadItem);
    }


}
